package edu.nwmissouri.galaxygames;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends FragmentActivity
{
    	//Class level builder and dialog for easy dismissal.
    	AlertDialog.Builder loadDialog;
    	AlertDialog eventLoadDialog;

    	//For easy access to the fragment container.
    	View container;

    	//Declare global fragment manager and app fragments.
    	FragmentManager fm;
    	Fragment lifeCounter;
    	Fragment evCalendar;
    	Fragment homeScreen;
    	Fragment announcementScreen;
    	Fragment contactInfo;
    	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    	//Normal onCreate instructions + get rid of title bar.
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_home);
		
		//Acquire handle of the homescreen and set it's background color.
		container = findViewById(R.id.container);
		container.getRootView().setBackgroundColor(Color.WHITE);
		
		//Acquire action bar, hide title bar, set navigation mode to tabs.
		final ActionBar ab = getActionBar();
		ab.setDisplayShowTitleEnabled(false);
		ab.setDisplayShowHomeEnabled(false);
		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		//Initialize all of the activity's fragment tabs.
		homeScreen = new HomeScreenFragment();
		lifeCounter = new LifeCounterFragment();
		evCalendar = new EventCalendarFragment();
		announcementScreen = new AnnouncementFragment();
		contactInfo = new ContactInfo();
		
		//Add the fragments as tabs to the action bar, each with their own icon, set their TabListener.
		ab.addTab(ab.newTab().setIcon(R.drawable.announcements).setTabListener(new Navigation(announcementScreen, this)));
		ab.addTab(ab.newTab().setIcon(R.drawable.events).setTabListener(new Navigation(evCalendar, this)));
		ab.addTab(ab.newTab().setIcon(R.drawable.life).setTabListener(new Navigation(lifeCounter, this)));
		ab.addTab(ab.newTab().setIcon(R.drawable.contact).setTabListener(new Navigation(contactInfo, this)));

		//Ensure the app begins with no tab selected.
		ab.selectTab(null);
		
		//Initialize fragment manager and put up the homescreen.
		fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.container, homeScreen);
		ft.commit();
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	public void makeEventLoadDialog()
	{
	  //build an alert dialog box to alert the user the event schedule is being retrieved
		loadDialog = new AlertDialog.Builder(this);
		loadDialog.setTitle("Retrieving Event Schedule");
		loadDialog.setMessage("Please Wait... \n");
		
		
		
		//build a cancel button on the alert dialog which will allow the user to stop attemping
		//to retrieve the schedule, and return to the main menu
		
		loadDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
					
				eventLoadDialog.dismiss();
				
			}
		});
		
		//Create and show an alert dialog.
		eventLoadDialog = loadDialog.create();
		eventLoadDialog.show();
	}
	
	
	public void closeEventLoadDialog(){
		eventLoadDialog.dismiss();
	}
	
	//Display an error if the network is not available
	public void makeNetworkErrorDialog(){
		AlertDialog.Builder noConnection = new AlertDialog.Builder(this);
		noConnection.setTitle("Network Error");
		noConnection.setMessage("\nThis device is not currently connected to the internet, and cannot retrieve the event schedule.\n");
		AlertDialog noConnectionDialog = noConnection.create();
		noConnectionDialog.show();
	}
	
	//Used to dismiss the network activity dialog after loading is finished.
	public void dismissNetworkDialog()
	{
	    eventLoadDialog.dismiss();
	}
	//Create and build an alert dialog showing the full information of the event.
	public void makeMoreInfoDialog(Event eventToShow)
	{
	    AlertDialog.Builder eventDialog = new AlertDialog.Builder(MainActivity.this);
	    eventDialog.setTitle(eventToShow.getTitle());
	    eventDialog.setMessage("Date: " + eventToShow.getDate() + "\n" + "Time: " + eventToShow.getTime() + "\nDescription: " + eventToShow.getDescription() + "\n");
	    eventDialog.setPositiveButton("Done", new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				//close the dialog box
				dialog.dismiss();
			}
		});
		
		AlertDialog eventDialogShow = eventDialog.create();
		eventDialogShow.show();
	}
	
	//Build and show a dialog that gives an accouncements details.
	public void makeAnnoucementDialog(Announcement announcementToShow){
		AlertDialog.Builder announcementDialog = new AlertDialog.Builder(MainActivity.this);
		announcementDialog.setTitle(announcementToShow.getTitle());
		announcementDialog.setMessage(announcementToShow.getDescription() + "\n");
		AlertDialog announcementDialogShow = announcementDialog.create();
		announcementDialogShow.show();
	}
	
	//Reset player one's life total to 20.
	public void resetPlayerOneLife(View view){
		LifeCounterFragment.playerOneLifeLV.setSelectionFromTop(20, LifeCounterFragment.selectionCenter);
	}
	
	//Reset player two's life total to 20.
	public void resetPlayerTwoLife(View view){
		LifeCounterFragment.playerTwoLifeLV.setSelectionFromTop(20, LifeCounterFragment.selectionCenter);
	}
	
	//Launch the galaxy games facebook page using an intent.
	public void launchFacebook(View v)
	{
	    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/rwtacooperider?fref=ts"));
	    startActivity(browserIntent);
	}
	
	
}
